extends Control

#引用场景里的节点
@onready var customer_spot = $CustomerSpot
@onready var dialogue_ui = $DialogueLayer
#假设这是当前的客人数据
@export var current_customer_data: CustomerData 
var collected_clue_ids: Array[String] = []#记录已有线索id
@onready var sfx_player: AudioStreamPlayer2D = $SFXPlayer

@export_group("音频配置")
@export var bgm_stream: AudioStream  #在编辑器里拖入音乐文件
@onready var bgm_player: AudioStreamPlayer2D = $BGM_Player

func _ready():
	if Dialogic.current_timeline != null:
		Dialogic.end_timeline()
	if get_tree().paused:
		get_tree().paused = false
	
	if is_instance_valid(SceneLoader) and SceneLoader.fade:
		SceneLoader.fade.hide()
		SceneLoader.fade.mouse_filter = Control.MOUSE_FILTER_IGNORE
	
	var saved_customer_path = Dialogic.VAR.get_variable("current_customer_id")
	
	if saved_customer_path and saved_customer_path != "":
		print("检测到读档数据，正在恢复客人...")
		var restored_data = load(saved_customer_path)
		current_customer_data = restored_data
		customer_spot.set_customer_data(restored_data)
		#恢复完一次后，清空变量，防止下次误判
		Dialogic.VAR.set_variable("current_customer_id", "")
		
	elif current_customer_data:
		print("加载场景默认配置的客人")
		customer_spot.set_customer_data(current_customer_data)
	
	else:
		print("警告：没有找到任何客人数据！")
	
	if current_customer_data:
		customer_spot.set_customer_data(current_customer_data)
	else:
		print("警告：MainBar没有配置开局的客人数据")
	
	if is_instance_valid(VN_SystemUI):
		VN_SystemUI.hide_hud()
		VN_SystemUI.force_hide_drink_menu()
	
	play_bgm_fade_in()
	
	await get_tree().process_frame
	#游戏开始，生成客人
	spawn_customer()
	
#播放背景音乐
func play_bgm_fade_in():
	#检查节点和音乐文件是否存在
	if bgm_player and bgm_stream:
		#设置音乐
		bgm_player.stream = bgm_stream
		
		#初始状态 (静音开始)
		bgm_player.volume_db = -80.0
		bgm_player.play()
		
		#淡入动画(从-80到-10，耗时1秒)
		var target_db = -10.0 
		var tween = create_tween()
		tween.tween_property(bgm_player, "volume_db", target_db, 1.0)
	else:
		print("注意：当前场景没有配置 BGM，或者找不到 BGM_Player 节点")
	

func spawn_customer():
	#设置客人图片
	customer_spot.texture = current_customer_data.texture
	#把数据传给customer_spot方便后续判定
	customer_spot.data = current_customer_data 
	#开始对话
	dialogue_ui.start_dialogue(current_customer_data.name, current_customer_data.dialog_start, current_customer_data.name_color)

#全局飘字系统
func show_floating_text(content: String, color: Color = Color.WHITE, pos: Vector2 = Vector2.ZERO):
	var label = Label.new()
	label.text = content
	
	#设置样式
	label.modulate = color
	label.z_index = 100 #保证最前
	label.add_theme_font_size_override("font_size", 24)
	label.add_theme_color_override("font_outline_color", Color.BLACK)
	label.add_theme_constant_override("outline_size", 4)
	
	#设置位置
	if pos == Vector2.ZERO:
		#如果没传位置，就显示在鼠标上方一点点
		label.global_position = get_global_mouse_position() + Vector2(-20, -50)
	else:
		#如果传了位置，就显示在指定位置
		label.global_position = pos
	
	#添加到主场景
	add_child(label)
	
	#动画
	var tween = create_tween()
	tween.set_parallel(true)
	tween.tween_property(label, "global_position:y", label.global_position.y - 50, 1.0)
	tween.tween_property(label, "modulate:a", 0.0, 1.0)
	tween.chain().tween_callback(label.queue_free)
	
func add_clue(clue_data: ClueData):
	print("正在尝试添加线索: ", clue_data.id)
	print("当前已收集列表: ", collected_clue_ids)
	
	if clue_data.id in collected_clue_ids:
		print("❌ 拦截：这个线索已经有了！")
		return
	
	#记录 ID
	collected_clue_ids.append(clue_data.id)
	
	#生成图标
	var item = load("res://scene/clue_item.tscn").instantiate()
	item.setup(clue_data)
	
	#加到侧边栏
	$ClueSidebar.add_child(item)
	
	#播放个获得物品的音效
	show_floating_text("获得了线索: " + clue_data.name, Color.CYAN)

#在重置游戏或换客人时清空侧边栏
func clear_clues():
	collected_clue_ids.clear()
	for child in $ClueSidebar.get_children():
		child.queue_free()

func play_sfx(stream: AudioStream, volume_db: float = 5.0, pitch_scale: float = 1.0):
	if stream:
		sfx_player.stream = stream
		sfx_player.volume_db = volume_db
		sfx_player.pitch_scale = pitch_scale
		sfx_player.play()
